﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class bagButton : MonoBehaviour, IPointerClickHandler
{
    
    private bag bagg;
    [SerializeField]
    private Sprite iconEmpty;
    [SerializeField]
    private Sprite iconFull;
    [SerializeField]
    private int bagId;
    public int MyBagId { get => bagId; set => bagId = value; }
    
    public bag MyBagg
    {
        get => bagg;

        set
        {
            if (value != null)
            {
                transform.GetChild(1).GetComponent<Image>().sprite = iconFull;
                transform.GetChild(1).GetComponent<Image>().color = Color.white;
            }
            else
            {
                transform.GetChild(1).GetComponent<Image>().sprite = iconEmpty;
                transform.GetChild(1).GetComponent<Image>().color = new Color(0,0,0,0);
            }
            bagg = value;
            
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if(eventData.button == PointerEventData.InputButton.Left)
        {
            if (inventoryScript.MyInstance.FromSlot != null && handScript.MyInstance.MyMovable != null && handScript.MyInstance.MyMovable is bag)
            {
                if (MyBagg != null)
                {
                    inventoryScript.MyInstance.swapBags(MyBagg, handScript.MyInstance.MyMovable as bag);
                }
                else
                {
                    bag tmp = handScript.MyInstance.MyMovable as bag;
                    tmp.MyBagButton = this;
                    tmp.use();
                    MyBagg = tmp;
                    handScript.MyInstance.Drop();
                    inventoryScript.MyInstance.FromSlot = null;
                }
                
            } 
           else if(Input.GetKey(KeyCode.LeftControl)|| Input.GetKey(KeyCode.RightControl))
            {
                handScript.MyInstance.takeMovable(MyBagg);
               
            }
            else if (bagg != null)
            {
                bagg.MyBagScript.Toggle();
            }
            

        }

    }
    public void deleteBag()
    {         
        inventoryScript.MyInstance.deleteBagInv(MyBagg);
        foreach (Item itm in bagg.MyBagScript.GetItems())
        {
            inventoryScript.MyInstance.AddItem(itm);
        }
        MyBagg.MyBagButton = null;
        MyBagg = null;
    }

}
